package cate.game.role.bag.kitchenware;

import cate.common.table.item.kitchenware.row.KitchenwareBaseRow;
import cate.common.table.item.kitchenware.row.KitchenwareSkillRow;
import cate.common.util.GameResult;
import cate.game.attr.KitchenwaresPowerAttrPart;
import cate.game.attr.PowerAttrPart;
import cate.game.play.support.PlayBuildContext;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.bag.hero.Hero;
import cate.game.role.bag.hero.OnHeroItem;
import com.fasterxml.jackson.annotation.JsonIgnore;
import cp.solution.util.collection.MapKit;
import easy.java.dev.note.NoteField;
import org.springframework.data.annotation.Transient;

import java.util.*;

public class KitchenwaresOn extends RoleDependent implements OnHeroItem<Kitchenware> {
    @JsonIgnore
    @Transient
    public transient Hero owner;

    @NoteField("身上的厨具")
    public Map<Byte, Kitchenware> kitchenwareMap;

    public void initialize(Hero owner, Role role){
        super.initialize(role);
        this.owner = owner;
        if(kitchenwareMap == null){
            kitchenwareMap = new HashMap<>();
        }
        kitchenwareMap.values().forEach(a -> a.initialize(role));
    }

    @Override
    public GameResult<Void> preCheckOn(PlayBuildContext ctx, byte pos, Kitchenware kitchenware){
        GameResult<Void> r = new GameResult<>();
        KitchenwareBaseRow row = kitchenware.getTpl(ctx);
        if (row == null) {
            return r.fail("厨具配置有误");
        }
        if (pos != row.pos) {
            return r.fail("穿戴部位错误");
        }
        return r.success();
    }

    @Override
    @JsonIgnore
    public KitchenwareBag getItemBag() {
        return role.getBag().kitchenware;
    }

    @Override
    public Map<Byte, Kitchenware> getPosItems() {
        return kitchenwareMap;
    }

    @Override
    public void doRealOn(byte pos, Kitchenware kitchenware) {
        kitchenwareMap.put(pos, kitchenware);
    }

    @Override
    public void doRealOff(byte pos, Kitchenware kitchenware) {
        kitchenwareMap.remove(pos);
    }

    @Override
    public PowerAttrPart getPowerAttrPart(PlayBuildContext ctx) {
        return new KitchenwaresPowerAttrPart(ctx, this);
    }

    @Override
    public KitchenwaresOn copy() {
        KitchenwaresOn cp = new KitchenwaresOn();
        cp.kitchenwareMap = MapKit.cloneDeep(this.kitchenwareMap);
        cp.initialize(this.owner, this.role);
        return cp;
    }

    @JsonIgnore
    public List<Integer> getSkills(PlayBuildContext ctx) {
        Set<Integer> skills = new HashSet<>();
        kitchenwareMap.values()
                .forEach(a -> {
                    if (a.skillIds != null && !a.skillIds.isEmpty()) {
                        for (Integer skillTid : a.skillIds) {
                            KitchenwareSkillRow skillRow = ctx.kitchenwareExcel().skill.get(skillTid);
                            if (skillRow != null && skillRow.skillId > 0) {
                                skills.add(skillRow.skillId);
                            }
                        }
                    }
                });
        return new ArrayList<>(skills);
    }
}
